package com.geoway.adf.report.excel.borderinfo;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geoway.adf.report.excel.conditionformat.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lzw
 * @Date: 2024/1/17 14:46
 * @Description: 边框信息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,
        property = "rangeType", visible = true)
@JsonSubTypes({@JsonSubTypes.Type(value = CellBorder.class, name = "cell"),
        @JsonSubTypes.Type(value = RangeBorder.class, name = "range")})
public abstract class BorderInfo {
    /**
     * cell     单个单元格
     * range    选区
     */
    private String rangeType;

    public static void main(String[] args) throws JsonProcessingException {
        String source = "[{\"rangeType\":\"cell\",\"value\":{\"row_index\":3,\"col_index\":3,\"l\":{\"style\":10,\"color\":\"rgb(255, 0, 0)\"},\"r\":{\"style\":10,\"color\":\"rgb(255, 0, 0)\"},\"t\":{\"style\":10,\"color\":\"rgb(255, 0, 0)\"},\"b\":{\"style\":10,\"color\":\"rgb(255, 0, 0)\"}}},{\"rangeType\":\"range\",\"borderType\":\"border-all\",\"style\":\"3\",\"color\":\"#0000ff\",\"range\":[{\"row\":[7,8],\"column\":[2,3]}]},{\"rangeType\":\"range\",\"borderType\":\"border-inside\",\"style\":\"3\",\"color\":\"#0000ff\",\"range\":[{\"row\":[7,8],\"column\":[8,9]}]}]";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(source);
        List<BorderInfo> borderInfos = new ArrayList<>();
        if(jsonNode.isArray()){
            for(int i=0; i<jsonNode.size(); i++){
                JsonNode node = jsonNode.get(i);
                borderInfos.add(mapper.treeToValue(node, BorderInfo.class));
            }
        }
        System.out.println(borderInfos);
    }
}